package com.hgd.security.evp.biz.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hgd.security.evp.biz.entity.EvpDepart;
import com.hgd.security.evp.biz.mapper.EvpDepartMapper;
import com.hgd.security.evp.biz.vo.DeptTree;
import com.hgd.security.evp.biz.vo.EvpDepartVo;
import com.hgd.security.evp.common.biz.BusinessBiz;
import com.hgd.security.evp.common.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 *
 * @author By Shang
 * @email ${email}
 * @version 2019-01-01 22:49:10
 */
@Service
public class EvpDepartBiz extends BusinessBiz<EvpDepartMapper, EvpDepart>{

    @Autowired
    private Environment environment;

    /**
     * 添加部门
     * @param EvpDepart 待添加部门
     * @return 结果
     */
    @Transactional
    public ObjectResultResponse<Void> addEvpDepart(EvpDepart EvpDepart) {
        ObjectResultResponse<Void> oResult=new ObjectResultResponse<>();

        if(StringUtil.isNotBlank(EvpDepart.getEvpCode())){
            // 如果不为空，则需要验证是否重复
            check(EvpDepart, oResult, false);
        }
        if (oResult.getStatus() == 400) {
            return oResult;
        }

        this.insertSelectiveBindCrt(EvpDepart);
        oResult.setMessage("添加部门成功");
        return oResult;
    }

    private void check(EvpDepart EvpDepart, ObjectResultResponse<Void> oResult, boolean isUpdate) {
        /*
         * 部门编辑不能重复
         */
        com.hgd.security.evp.biz.entity.EvpDepart queryEvpDepart = new EvpDepart();
        queryEvpDepart.setEvpCode(EvpDepart.getEvpCode());
        queryEvpDepart.setIsDeleted(BooleanUtil.BOOLEAN_FALSE);
        com.hgd.security.evp.biz.entity.EvpDepart depart = this.selectOne(queryEvpDepart);

        if (EmptyUtil.isNotEmpty(depart)) {
            if (isUpdate) {
                if (StringUtil.equals(depart.getEvpCode(), EvpDepart.getEvpCode()) && (int) EvpDepart.getId() != depart
                    .getId()) {
                    oResult.setMessage("该部门编辑已存在");
                    oResult.setStatus(400);
                }
            }else{
                if (StringUtil.equals(depart.getEvpCode(), EvpDepart.getEvpCode())) {
                    oResult.setMessage("该部门编辑已存在");
                    oResult.setStatus(400);
                }
            }
        }
    }

    /**
     * 获取部门树
     * @return 部门树
     */
    public List<DeptTree> getDepartTree() {
        List<EvpDepart> allDepart = getAllDepart();

        List<DeptTree> departTrees=new ArrayList<>();
        allDepart.forEach(depart -> {
            DeptTree deptTree = new DeptTree(depart.getId(), depart.getParentId(), depart.getEvpName());
            deptTree.setState(environment.getProperty("treeMenuClosedStatus"));
            departTrees.add(deptTree);
        });

        return TreeUtil.buildTree(departTrees, -1);
    }

    /**
     * 获取全部在使用部门
     * @return 全部在使用部门
     */
    public List<EvpDepart> getAllDepart(){
        Example example=new Example(EvpDepart.class);

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
        example.setOrderByClause("order_num ASC");
        List<EvpDepart> EvpDeparts = this.selectByExample(example);
        if(EmptyUtil.isEmpty(EvpDeparts)){
            return new ArrayList<>();
        }

        return EvpDeparts;
    }

    /**
     * 分页获取对象
     *
     * @param depart
     * @param page
     * @param rows
     * @return
     */
    public ListResult<EvpDepart> getDeptList(EvpDepart depart, Integer page, Integer rows) {
        Example example = new Example(EvpDepart.class);
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("isDeleted", BooleanUtil.BOOLEAN_FALSE);
        if (StringUtil.isNotBlank(depart.getEvpName())) {
            criteria.andLike("evpName", "%" + depart.getEvpName() + "%");
        }
        if (StringUtil.isNotBlank(depart.getEvpCode())) {
            criteria.andLike("evpCode", "%" + depart.getEvpCode() + "%");
        }
        if (StringUtil.isNotBlank(depart.getEvpType())) {
            criteria.andEqualTo("evpType", depart.getEvpType());
        }
        if (StringUtil.isNotBlank(depart.getParentId())) {
            criteria.andEqualTo("parentId", depart.getParentId());
        }

        example.setOrderByClause("order_num asc");

        Page<Object> pageInfo = PageHelper.startPage(page, rows);
        List<EvpDepart> EvpDeparts = this.selectByExample(example);

        if (EmptyUtil.isNotEmpty(EvpDeparts)) {
            return new ListResult<>(pageInfo.getTotal(), EvpDeparts);
        }

        return new ListResult<>(0, new ArrayList<>());
    }

    /**
     * 分页获取对象
     *
     * @param depart
     * @param page
     * @param rows
     * @return
     */
    public ListResult<EvpDepartVo> getDeptListBindParent(EvpDepart depart, Integer page, Integer rows) {
        if (StringUtil.isNotBlank(depart.getEvpName())) {
            depart.setEvpName("%" + depart.getEvpName() + "%");
        }
        if (StringUtil.isNotBlank(depart.getEvpCode())) {
            depart.setEvpCode("%" + depart.getEvpCode() + "%");
        }

        Page<Object> pageInfo = PageHelper.startPage(page, rows);
        List<EvpDepartVo> EvpDeparts = this.mapper.getEdeptLitBindParent(depart);

        if (EmptyUtil.isNotEmpty(EvpDeparts)) {
            return new ListResult<>(pageInfo.getTotal(), EvpDeparts);
        }

        return new ListResult<>(0, new ArrayList<>());
    }

    public EvpDepartVo getDeptById(Integer id) {
        return this.mapper.getEDeptByIdBindParent(id);
    }

    @Transactional
    public ObjectResultResponse<Void> updateEvpDepart(EvpDepart EvpDepart) {
        ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

        check(EvpDepart, restResult, true);
        if (restResult.getStatus() == 400) {
            return restResult;
        }

        this.updateSelectiveByIdBindUpd(EvpDepart);

        restResult.setStatus(200);
        restResult.setMessage("编辑部门成功");

        return restResult;
    }

    @Transactional
    public ObjectResultResponse<Void> delDeptById(Integer id) {
        ObjectResultResponse<Void> restresult = new ObjectResultResponse<>();

        // 如果该部门下存在子部门则不能进行删除
        EvpDepart queryDept = new EvpDepart();
        queryDept.setIsDeleted(BooleanUtil.BOOLEAN_FALSE);
        queryDept.setParentId(String.valueOf(id));

        ListResult<EvpDepart> deptList = getDeptList(queryDept, 1, 1);
        if (EmptyUtil.isNotEmpty(deptList.getRows())) {
            restresult.setStatus(400);
            restresult.setMessage("该部门下存在子部门，不能进行删除。");
            return restresult;
        }

        EvpDepart delDept = new EvpDepart();
        delDept.setIsDeleted(BooleanUtil.BOOLEAN_TRUE);
        delDept.setId(id);
        this.updateSelectiveByIdBindUpd(delDept);
        restresult.setStatus(200);
        restresult.setMessage("删除部门成功");
        return restresult;
    }

    /**
     * 按ID查询部门
     * @param ids
     */
    public List<EvpDepart> selectbyids(String ids) {
        return this.mapper.selectByIds(ids);
    }

    @Transactional
    public ObjectResultResponse<Void> updateEDepart(EvpDepart eDepart) {
        ObjectResultResponse<Void> restResult = new ObjectResultResponse<>();

        check(eDepart, restResult, true);
        if (restResult.getStatus() == 400) {
            return restResult;
        }

        this.updateSelectiveByIdBindUpd(eDepart);

        restResult.setStatus(200);
        restResult.setMessage("编辑部门成功");

        return restResult;
    }
}